﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SyntaxHighlight;
using Microsoft.FSharp.Collections;

namespace TestApp
{
	/// <summary>
	/// Parser for SQL code
	/// </summary>
	public class TokenParser : ISyntaxLexer
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="text"></param>
		/// <param name="caret_position"></param>
		public override void Parse ( string text, int caret_position )
		{
			_tokens.Clear ();
			try
			{
				var lexbuf = SQLParser.tokenizeText ( text );
				var token = SQLParser.token ( lexbuf );

				while ( !token.IsEOF )
				{
					CodeTokenType type = CodeTokenType.None;

					if ( token.IsSELECT || token.IsWHERE || token.IsRIGHT || token.IsORDER || token.IsOR || token.IsON ||
						token.IsLEFT || token.IsJOIN || token.IsINNER || token.IsFROM || token.IsDESC || token.IsBY ||  token.IsAND || token.IsOR)
						type = CodeTokenType.Keyword;
					if ( token.IsID )
						type = CodeTokenType.Indentifier;
					if ( token.IsINT || token.IsFLOAT )
						type = CodeTokenType.Number;
					if ( token.IsCOMMENT )
						type = CodeTokenType.Comment;

					_tokens.Add ( new CodeToken () { TokenType = type, Start = lexbuf.StartPos.AbsoluteOffset, End = lexbuf.EndPos.AbsoluteOffset } );

					token = SQLParser.token ( lexbuf );
				}
			}
			catch
			{ }
		}

		public override System.Windows.Input.Key SuggestionListTriggerKey
		{
			get
			{
				return System.Windows.Input.Key.OemPeriod;
			}
		}

		public override bool CanShowSuggestionList ( int caret_position )
		{
			return true;
		}
	}
}
